package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.AppMapper;
import com.winshare.saleAssistant.domain.dto.req.*;
import com.winshare.saleAssistant.domain.dto.resp.AppRespDTO;
import com.winshare.saleAssistant.domain.entity.AppDO;
import com.winshare.saleAssistant.domain.entity.AppMenuDO;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.AppMapStruct;
import com.winshare.saleAssistant.service.AppService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppServiceImpl implements AppService {

    private final AppMapper appMapper;

    public AppServiceImpl(AppMapper appMapper) {
        this.appMapper = appMapper;
    }

    @Override
    public void saveApp(AppCreateDTO appCreateDTO) {
        AppDO appDO = AppMapStruct.INSTANCE.fromAppCreateDTO(appCreateDTO);
        if(checkAppExist(appDO)) {
            throw new BusinessException("此应用在系统中已存在");
        }
        appMapper.saveApp(appDO);
    }

    @Override
    public void updateApp(AppUpdateDTO appUpdateDTO) {
        AppDO appDO = AppMapStruct.INSTANCE.fromAppUpdateDTO(appUpdateDTO);
        if(checkAppExist(appDO)) {
            throw new BusinessException("此应用在系统中已存在");
        }
        appMapper.updateApp(appDO);
    }

    @Override
    public List<AppRespDTO> getAppList() {
        List<AppDO> appDOList = appMapper.getAppDOList();
        if(CollectionUtils.isEmpty(appDOList)) {
            return new ArrayList<>();
        }
        List<AppRespDTO> appRespDTOList = AppMapStruct.INSTANCE.fromAppDOList(appDOList);
        return appRespDTOList;
    }

    @Override
    public AppRespDTO getAppDetail(BaseIdDTO baseIdDTO) {
        AppDO appDO = AppMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        AppDO appDOInSql = appMapper.getAppDO(appDO);
        if(null == appDOInSql) {
            throw new BusinessException("当前应用不存在");
        }
        AppRespDTO appRespDTO = AppMapStruct.INSTANCE.fromAppDO(appDOInSql);
        return appRespDTO;
    }

    @Override
    public void deleteApp(BaseIdDTO baseIdDTO) {
        AppDO appDO = AppMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        AppDO appDOInSql = appMapper.getAppDO(appDO);
        if(null == appDOInSql) {
            throw new BusinessException("当前应用不存在");
        }
        appMapper.deleteApp(appDO);
    }

    @Override
    public void addAppMenuList(AppMenuUpdateDTO appMenuUpdateDTO) {
        AppMenuDO appMenuDO = AppMapStruct.INSTANCE.fromAppMenuUpdateDTO(appMenuUpdateDTO);
        List<MenuDTO> menuDTOList = appMenuUpdateDTO.getMenuList();
        if(!CollectionUtils.isEmpty(menuDTOList)) {
            List<AppMenuDO> appMenuDOList = menuDTOList.stream()
                    .map(item -> {
                        AppMenuDO appMenuDO1 = new AppMenuDO();
                        appMenuDO1.setAppId(appMenuUpdateDTO.getAppId());
                        appMenuDO1.setAppName(appMenuUpdateDTO.getAppName());
                        appMenuDO1.setMenuId(item.getId());
                        appMenuDO1.setMenuName(item.getMenuName());
                        appMenuDO1.setParentMenuId(item.getParentMenuId());
                        appMenuDO1.setLink(item.getLink());
                        return appMenuDO1;
                    }).collect(Collectors.toList());
            appMapper.deleteAppMenuByApp(appMenuDO);
            appMapper.saveAppMenu(appMenuDOList);
        }
    }

    @Override
    public List<MenuDTO> getMenuListByApp(BaseIdDTO baseIdDTO) {
        AppMenuDO appMenuDO = new AppMenuDO();
        appMenuDO.setAppId(baseIdDTO.getId());
        return appMapper.getMenuListByApp(appMenuDO);
    }

    @Override
    public List<MenuDTO> getMenuListByAppV2(BaseIdDTO baseIdDTO) {
        List<MenuDTO> menuDTOList = getMenuListByApp(baseIdDTO);
        // 转化成一棵树的形式
        if(!CollectionUtils.isEmpty(menuDTOList)) {
            List<MenuDTO> menuTreeDTOList = buildTree(menuDTOList);
            return menuTreeDTOList;
        }
        return new ArrayList<>();
    }

    private List<MenuDTO> buildTree(List<MenuDTO> menuDTOList) {
        List<MenuDTO> treeRespDTOList = new ArrayList<>();
        for(MenuDTO menuDTO : menuDTOList){
            if(menuDTO.getParentMenuId().intValue() == 0){
                menuDTO = buildChildTree(menuDTO,menuDTOList);
                treeRespDTOList.add(menuDTO);
            }
        }
        return treeRespDTOList;
    }

    private MenuDTO buildChildTree(MenuDTO pNode,List<MenuDTO> menuDTOList) {
        List<MenuDTO> childMenuDTOList = new ArrayList<>();
        for(MenuDTO menuDTO : menuDTOList) {
            if(menuDTO.getParentMenuId().intValue() == pNode.getId()) {
                childMenuDTOList.add(buildChildTree(menuDTO,menuDTOList));
            }
        }
        pNode.setChildMenuTreeDTOList(childMenuDTOList);
        return pNode;
    }

    private boolean checkAppExist(AppDO appDO) {
        return appMapper.countByApp(appDO) > 0;
    }
}
